<?php

namespace app\common\model;

use think\Model;

/**
 * 商品模型
 */
class Goods Extends Model
{

    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';

    protected $append = [
        'category_id_text',
        'province_text',
        'city_text',
        'area_text',
        'updatetime_text',
        'shengyujihui'
    ];

    public function getCategoryIdTextAttr($value, $data)
    {
        $value = $value ? $value : $data['category_id'];
        $categoryM = new Category();
        $list = $categoryM->getList('default');
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getProvinceTextAttr($value, $data)
    {
        $value = $value ? $value : $data['province'];
        $model = new Region();
        $list = $model->getList(1);
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getCityTextAttr($value, $data)
    {
        $value = $value ? $value : $data['city'];
        $model = new Region();
        $list = $model->getList(2);
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getAreaTextAttr($value, $data)
    {
        $value = $value ? $value : $data['area'];
        $model = new Region();
        $list = $model->getList(3);
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getUpdatetimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['updatetime'];
        $data = get_last_time($value);
        return $data;
    }

    public function getShengyujihuiAttr($value, $data){
        return $data['total_num'] - $data['changes_num'];
    }





}